/* 
 * File:   Game.cpp
 * Author: agu
 * 
 * Created on 22 de mayo de 2014, 14:45
 */

#include "Game.h"
#include "InitializationException.h"
#include "Directories.h"

Game::Game(string name) : window(name) {
    if (!this->window.init())
        throw InitializationException("Error al iniciar al juego");
    this->renderer = SDL_CreateRenderer(window.getWindowSDL(), -1, SDL_RENDERER_ACCELERATED);
    this->loadGraphics();
    this->runnig = false;
}

Game::Game(Window& windows, SDL_Renderer* renderer) {
    this->window = windows;
    this->renderer = renderer;
    this->runnig = false;
    //this->init();
}
//este esta de prueba
void Game::free() {
     this->active=false;
    this->freeMap();
    this->freeAnimations();
    //SDL_DestroyRenderer(this->renderer);
}

Game::~Game() {
    std::cout << "destruir juego" << std::endl;
    this->freeMap();
    this->freeAnimations();
    SDL_DestroyRenderer(this->renderer);
}
//este esta de prueba
void Game::init() {

    this->loadGraphics();
    //this->runnig = false;
}

void Game::run() {
    //this->init();
    this->runnig = true;
    //this->active=true;
    while (this->runnig) {
        this->input();
        this->update();
        this->render();
    }
    this->active=false;
    //this->free();
}

void Game::input() {
    while (SDL_PollEvent(&this->event)) {
        this->handleEvent();
    }
}

void Game::handleEvent() {

    switch (this->event.type) {
        case SDL_QUIT:
            this->runnig = false;
            this->window.kill();
            break;
        case SDL_WINDOWEVENT:
            this->window.handleEvent(this->event.window);
            break;

    }

}

void Game::render() {
    //ver si esta linea es necesaria
    SDL_SetRenderDrawColor(this->renderer, 0xFF, 0xFF, 0xFF, 0xFF);
    SDL_RenderClear(this->renderer);
    this->renderMap();
    this->renderAnimations();
    SDL_RenderPresent(this->renderer);
}

void Game::renderMap() {
    //TODO:ver bien esto como se trata
    this->wall->resize(window.getWidth(), window.getHeight());
    this->wall->render();
    for (unsigned int i = 0; i < this->items.size(); i++) {
        this->items.at(i)->render();
    }
}

void Game::renderAnimations() {
//    for (int i = 0; i < this->animations.size(); i++) {
//        this->animations.at(i)->render();
//    }
    this->player->render();
}

void Game::updateMap() {
    //Aca se actualiza el mapa
    //Cambiar las imagenes que se estan mostrando
    //Actualizar la posicion de las animaciones
    this->player->updatePosition(); //(0,0) esta harcodeado, luego debe recibir
                                    // esta info desde el tilemap
}

void Game::updateMovements() {
    
    if(this->player != NULL)
        this->player->updateMovement();
//    for (int i = 0; i < this->animations.size(); i++) {
//        this->animations.at(i)->update();
//    }
}

void Game::update() {
    this->updateMovements();
    //comunicacion con el servidor
    this->updateMap();
}

void Game::loadMap() {

    this->items.push_back(new Texture(this->renderer, DIR_IMG_MAPS_ITEMS + "box.png", 20, 0));
    this->items.push_back(new Texture(this->renderer, DIR_IMG_MAPS_ITEMS + "box.png", 20, 64));
    this->items.push_back(new Texture(this->renderer, DIR_IMG_MAPS_ITEMS + "box.png", 20, 64 * 2));
    this->items.push_back(new Texture(this->renderer, DIR_IMG_MAPS_ITEMS + "box.png", 84, 64 * 2));
    this->items.push_back(new Texture(this->renderer, DIR_IMG_MAPS_ITEMS + "box.png", 130, 64 * 2));
    this->wall = new Texture(this->renderer, DIR_IMG_MAPS_BACKGROUND + "fondo_2.png", 0, 0);
}

void Game::loadAnimations() {
    //La animacion se crea en la esquina superior izquierda de la pantalla
    //this->animations.push_back(new Animation(this->renderer, DIR_IMG_SPRITES + "monster.png", 0, 0));
    this->player=new Animation(this->renderer, DIR_IMG_SPRITES + "sprite_red.png", 100, 100);
}

void Game::loadGraphics() {
    this->loadMap();
    this->loadAnimations();
}

void Game::freeMap() {
    delete this->wall;
    for (unsigned int i = 0; i < this->items.size(); i++) {
        delete this->items.at(i);
    }
}

void Game::freeAnimations() {
//    for (int i = 0; i < this->animations.size(); i++) {
//        delete this->animations.at(i);
//    }
    if(this->player != NULL)
        delete this->player;
}